<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分类名字" prop="attrName">
      <el-input v-model="dataForm.attrName" placeholder="分类名字"></el-input>
    </el-form-item>
    <el-form-item label="是否可供检索" prop="searchType">
      <el-input v-model="dataForm.searchType" placeholder="是否可供检索"></el-input>
    </el-form-item>
    <el-form-item label="属性类型" prop="attrType">
      <el-input v-model="dataForm.attrType" placeholder="属性类型"></el-input>
    </el-form-item>
    <el-form-item label="三级分类id" prop="categoryId">
      <el-input v-model="dataForm.categoryId" placeholder="三级分类id"></el-input>
    </el-form-item>
    <el-form-item label="状态  0：禁用   1：正常" prop="isStatus">
      <el-input v-model="dataForm.isStatus" placeholder="状态  0：禁用   1：正常"></el-input>
    </el-form-item>
    <el-form-item label="注册时间" prop="gmtCreate">
      <el-input v-model="dataForm.gmtCreate" placeholder="注册时间"></el-input>
    </el-form-item>
    <el-form-item label="更新时间" prop="gmtModified">
      <el-input v-model="dataForm.gmtModified" placeholder="更新时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          attrId: 0,
          attrName: '',
          searchType: '',
          attrType: '',
          categoryId: '',
          isStatus: '',
          gmtCreate: '',
          gmtModified: ''
        },
        dataRule: {
          attrName: [
            { required: true, message: '分类名字不能为空', trigger: 'blur' }
          ],
          searchType: [
            { required: true, message: '是否可供检索不能为空', trigger: 'blur' }
          ],
          attrType: [
            { required: true, message: '属性类型不能为空', trigger: 'blur' }
          ],
          categoryId: [
            { required: true, message: '三级分类id不能为空', trigger: 'blur' }
          ],
          isStatus: [
            { required: true, message: '状态  0：禁用   1：正常不能为空', trigger: 'blur' }
          ],
          gmtCreate: [
            { required: true, message: '注册时间不能为空', trigger: 'blur' }
          ],
          gmtModified: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.attrId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.attrId) {
            this.$http({
              url: this.$http.adornUrl(`/ly/attr/info/${this.dataForm.attrId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.attrName = data.attr.attrName
                this.dataForm.searchType = data.attr.searchType
                this.dataForm.attrType = data.attr.attrType
                this.dataForm.categoryId = data.attr.categoryId
                this.dataForm.isStatus = data.attr.isStatus
                this.dataForm.gmtCreate = data.attr.gmtCreate
                this.dataForm.gmtModified = data.attr.gmtModified
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/ly/attr/${!this.dataForm.attrId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'attrId': this.dataForm.attrId || undefined,
                'attrName': this.dataForm.attrName,
                'searchType': this.dataForm.searchType,
                'attrType': this.dataForm.attrType,
                'categoryId': this.dataForm.categoryId,
                'isStatus': this.dataForm.isStatus,
                'gmtCreate': this.dataForm.gmtCreate,
                'gmtModified': this.dataForm.gmtModified
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
